<?php


/*
 * Created on Dec 3, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

if ($_POST["spotid"]) {
	$return_val = null;
	$con = mysql_connect("localhost", "root", "bytewalla5") or die(mysql_error());
	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("wsn", $con);

	$result = mysql_query("SELECT distinct timestamp FROM data where spot_id='" . $_POST["spotid"] . "'");
	while ($row = mysql_fetch_array($result)) {
		$return_val .= "<option value='" . $row["timestamp"] . "'>" . $row["timestamp"] . "</option>";
	}

	mysql_close($con);
	echo $return_val;
}

if ($_POST["action"] == "graph") {
	header("HTTP/1.0 307 Temporary redirect");
	header("Location:graph.php");

} else {

	$sensor_id = $_POST['sunspot'];
	$start = $_POST['start'];
	$stop = $_POST['end'];
	$desteid = $_POST['desteid'];

	$con = mysql_connect("localhost", "root", "bytewalla5");
	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("wsn", $con);

	// select last 5 entries
	//SELECT distinct timestamp,light FROM data WHERE spot_id="30" AND timestamp between "2011-11-30 08:15:41" and "2011-11-30 08:16:05" order by timestamp
	$query = "SELECT distinct timestamp,light,temperature,battery FROM data WHERE spot_id=\"$sensor_id\" AND timestamp between \"$start\" and \"$stop\" order by timestamp ";
	//echo $query;
	$res = mysql_query($query);
	// put all the data inside an array
	$arr = array ();
	while ($obj = mysql_fetch_object($res)) {
		$arr[] = $obj;
	}

	for ($i = 1; $i <= sizeof($arr); $i++) {
		$timestamp = $arr[$i -1]->timestamp;
		$light = $arr[$i -1]->light;
		$temperature = $arr[$i -1]->temperature;

		$battery = $arr[$i -1]->battery;

		$data .= $timestamp . "\t" . $light . "\t\t" . $temperature . "\t\t" . $battery . "\n";
	}

	mysql_close($con);
	//print $data;
	
	$filename =  time(). ".txt";
	$fileposition = "/var/www/wsn/download/" . $filename;

	$f = fopen($fileposition, 'w');
	fwrite($f, "Data collected from ".$start . " to " . $stop . " by senor " . $sensor_id."\n");
	fwrite($f, "Timestamp\t\t" . "Light\t\t" . "Temperature\t" . "Battery\t\t\n");
	fwrite($f, $data);
	fclose($f);

	if ($_POST["action"] == "download") {
		if ($fd = fopen($fileposition, "r")) {
			$fsize = filesize($fileposition);
			$path_parts = pathinfo($fileposition);

			header("Content-type: application/octet-stream");
			if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT'])) {
				header('Content-Disposition:  attachment; filename="' . $fileposition . '"');
			}
			elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {
				header('Content-Disposition: attachment; filename*="utf8' . $fileposition . '"');
			} else {
				header('Content-Disposition: attachment; filename="' . $fileposition . '"');
			}
			header("Content-length: $fsize");
    		header("Cache-control: private"); //use this to open files directly
			while (!feof($fd)) {
				$buffer = fread($fd, 2048);
				echo $buffer;
			}
		}
		fclose($fd);

	} else
		if ($_POST["action"] == "distribute") {
			include_once ("httpsqs/httpsqs_client.php");
			$httpsqs_host = "localhost";
			$httpsqs_port = "1218";
			$httpsqs_auth = "";
			$httpsqs_charset = "utf-8";
			$httpsqs = new httpsqs($httpsqs_host, $httpsqs_port, $httpsqs_auth, $httpsqs_charset);
			//curl -d "dtncp /home/hao/test.txt dtn://hao.bytewalla.com"  "http://localhost:1218/?charset=utf-8&name=&opt=put"
			$httpsqs->reset("text_java_request");
			$cmd = "dtncp ".$fileposition." ". $desteid;
			$result = $httpsqs->put("text_java_request", $cmd);
			header("Location:dtncpres.php");
		}

}
?>
